//
//  NSString+Extension.swift
//  0330来源处理
//
//  Created by Tang on 16/3/30.
//  Copyright © 2016年 大天朝. All rights reserved.
//

import Foundation

extension String {
    func sinaSource() -> String {
        
        let patten = ">(.*?)<"
        
        let regular = try! NSRegularExpression(pattern: patten, options: NSRegularExpressionOptions.DotMatchesLineSeparators)
        /*
        NSTextCheckingResult?
        public var numberOfRanges: Int { get } 匹配到的范围数量
        public func rangeAtIndex(idx: Int) -> NSRange 通过下标来获取范围
        */
        let result = regular.firstMatchInString(self, options: NSMatchingOptions(rawValue: 0), range: NSRange(location: 0, length: self.characters.count))
        
        //匹配到的范围数量
        let count = result?.numberOfRanges ?? 0
        if count > 1 {
            let range = result!.rangeAtIndex(1)
            return (self as NSString).substringWithRange(range)
        }
        return "未知来源"
    }
    
}